package dianmobile.eistarandroid.ui.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import dianmobile.eistarandroid.R;
import dianmobile.eistarandroid.beans.PostsIntroduction;
import dianmobile.eistarandroid.network.request.RequestFactory;
import dianmobile.eistarandroid.ui.activities.UserInfoActivity;
import dianmobile.eistarandroid.utils.ConstantsData;
import dianmobile.eistarandroid.utils.DataUtils;

/**
 * Created by YY on 2015/9/6.
 */
public class BoardContentAdapter extends BaseAdapter {
    List<PostsIntroduction> contentList;
    Context context;
    LayoutInflater inflater;

    public BoardContentAdapter(Context context, List<PostsIntroduction> list) {
        this.contentList = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    private void getUserHeadFromNetwork(ImageView imageView, String imageName ){
        String url = DataUtils.getUserHeadUrlFromName(imageName);
        RequestFactory.makeNetworkImage(imageView, url, R.drawable.default_face, 0);
    }

    @Override
    public int getCount() {
        return contentList.size();
    }

    @Override
    public Object getItem(int position) {
        return contentList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView == null) {
            convertView = inflater.inflate(R.layout.listitem_board_content, null);
            viewHolder = new ViewHolder();
            viewHolder.authorHead = (CircleImageView)convertView.findViewById(R.id.iv_user_head);
            viewHolder.titleTv = (TextView)convertView.findViewById(R.id.tv_title);
            viewHolder.authorTv = (TextView)convertView.findViewById(R.id.tv_author);
            viewHolder.replyCountTv = (TextView)convertView.findViewById(R.id.tv_reply_count);
            viewHolder.postTime = (TextView)convertView.findViewById(R.id.tv_post_time);
            viewHolder.summaryTv = (TextView)convertView.findViewById(R.id.tv_summary);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder)convertView.getTag();
        }

        final PostsIntroduction item = contentList.get(position);
        getUserHeadFromNetwork(viewHolder.authorHead, item.getAuthor());
        viewHolder.titleTv.setText(item.getTitle());
        viewHolder.authorTv.setText(item.getAuthor());
        viewHolder.replyCountTv.setText(item.getReplyCount());
        viewHolder.postTime.setText(item.getPostTime());
        viewHolder.summaryTv.setText(item.getSummary());
        if(item.isRead()) {
            viewHolder.titleTv.setAlpha((float)0.54);
        } else {
            viewHolder.titleTv.setAlpha((float)1.0);
        }

        viewHolder.authorHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, UserInfoActivity.class);
                intent.putExtra(ConstantsData.EXTRA_USER_NAME, item.getAuthor());
                context.startActivity(intent);
            }
        });
        return convertView;

    }

    class ViewHolder {
        CircleImageView authorHead;
        TextView titleTv;
        TextView authorTv;
        TextView replyCountTv;
        TextView postTime;
        TextView summaryTv;
    }
}
